package com.community.web.controller.community;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.community.body.domain.ZyCommunity;
import com.community.body.domain.ZyUnit;
import com.community.body.service.IZyUnitService;
import com.community.common.utils.DateUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.community.common.annotation.Log;
import com.community.common.core.controller.BaseController;
import com.community.common.core.domain.AjaxResult;
import com.community.common.enums.BusinessType;

import com.community.common.utils.poi.ExcelUtil;
import com.community.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 单元 Controller
 * 
 * @author community
 * @date 2024-04-09
 */
@RestController
@RequestMapping("/community/unit")
public class ZyUnitController extends BaseController
{
    @Autowired
    private IZyUnitService zyUnitService;

    /**
     * 查询单元 列表
     */
    @PreAuthorize("@ss.hasPermi('community:unit:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZyUnit zyUnit) {
        startPage();
        List<ZyUnit> list = zyUnitService.selectZyUnitList(zyUnit);
        return getDataTable(list);
    }

    /**
     * 查询三级联动 列表
     */
    @PreAuthorize("@ss.hasPermi('community:unit:threelist')")
    @GetMapping("/threeList")
    public AjaxResult threeList() {
        return success(zyUnitService.selectZyList());
    }

    /**
     * 导出单元 列表
     */
    @PreAuthorize("@ss.hasPermi('community:unit:export')")
    @Log(title = "单元 ", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZyUnit zyUnit) {
        String[] strings = zyUnit.getUnitCode().split(",");
        Long[] longs = new Long[strings.length];

        // 遍历字符串数组，将每个字符串转换为long
        for (int i = 0; i < strings.length; i++) {
            longs[i] = Long.parseLong(strings[i]);
        }
        List<ZyUnit> list = zyUnitService.selectZyunitExport(longs);
//        List<ZyUnit> list = zyUnitService.selectUnit(zyUnit);
        ExcelUtil<ZyUnit> util = new ExcelUtil<ZyUnit>(ZyUnit.class);
        util.exportExcel(response, list, "单元 数据");
    }

    /**
     * 获取单元 详细信息
     */
    @PreAuthorize("@ss.hasPermi('community:unit:query')")
    @GetMapping(value = "/{unitId}")
    public AjaxResult getInfo(@PathVariable("unitId") Long unitId) {
        return success(zyUnitService.selectZyUnitByUnitId(unitId));
    }

    @Log(title = "单元管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:unit:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ZyUnit> util = new ExcelUtil<>(ZyUnit.class);
        List<ZyUnit> unitList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = zyUnitService.importUnit(unitList, updateSupport, operName);
        return success(message);
    }

    /**
     * 新增单元
     */
    @PreAuthorize("@ss.hasPermi('community:unit:add')")
    @Log(title = "单元 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZyUnit zyUnit) throws ParseException {

        return toAjax(zyUnitService.insertZyUnit(zyUnit));
    }

    /**
     * 修改单元
     */
    @PreAuthorize("@ss.hasPermi('community:unit:edit')")
    @Log(title = "单元 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZyUnit zyUnit) {
        zyUnit.setUpdateTime(DateUtils.getNowDate());
        return toAjax(zyUnitService.updateZyUnit(zyUnit));
    }

    /**
     * 删除单元
     */
    @PreAuthorize("@ss.hasPermi('community:unit:remove')")
    @Log(title = "单元 ", businessType = BusinessType.DELETE)
    @DeleteMapping("/{unitIds}")
    public AjaxResult remove(@PathVariable Long[] unitIds) {
        return toAjax(zyUnitService.deleteZyUnitByUnitIds(unitIds));
    }
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<ZyUnit> util = new ExcelUtil<ZyUnit>(ZyUnit.class);
        util.importTemplateExcel(response, "单元数据");
    }
}
